<?php
namespace app\api\service\ceping;

class JiatingjiaoyuService {
    // 教育方式维度配置
	//采用李克特5级量表（1=完全不符合，5=完全符合）：positive正向negative反向	//正向题直接计分，反向题则需要转换分数（例如，5分制中选1则计5分，选5则计1分）。每个维度的得分需要加权，可能某些维度对总体评估影响更大。
    public $dimensions = [
        'authoritative' => [
            'name' => '权威型',
            'questions' => [
                ['id' => 1, 'text' => '我会与孩子共同制定家庭规则', 'type' => 'positive'],
                ['id' => 2, 'text' => '我经常忽视孩子的情绪需求', 'type' => 'negative'],
                ['id' => 3, 'text' => '我会解释规则背后的原因', 'type' => 'positive']
            ],
            'weight' => 1.2
        ],
        'authoritarian' => [
            'name' => '专制型',
            'questions' => [
                ['id' => 4, 'text' => '孩子必须无条件服从我的要求', 'type' => 'positive'],
                ['id' => 5, 'text' => '我很少给孩子选择的权利', 'type' => 'positive']
            ],
            'weight' => 1.1
        ],
        'permissive' => [
            'name' => '放任型', 
            'questions' => [
                ['id' => 6, 'text' => '我通常不干涉孩子的行为', 'type' => 'positive'],
                ['id' => 7, 'text' => '我会严格执行定下的规矩', 'type' => 'negative']
            ],
            'weight' => 1.0
        ],
        'neglectful' => [
            'name' => '忽视型',
            'questions' => [
                ['id' => 8, 'text' => '我经常不知道孩子最近在做什么', 'type' => 'positive'],
                ['id' => 9, 'text' => '我每天都会与孩子深入交流', 'type' => 'negative']
            ],
            'weight' => 1.0
        ]
    ];

    // 生成测评问卷
    public function generateTest() {
        $questionnaire = [];
        foreach ($this->dimensions as $dim) {
            shuffle($dim['questions']);
            $questionnaire = array_merge($questionnaire, $dim['questions']);
        }
        return $questionnaire;
    }

    // 计算教育方式得分
    public function calculateScore($answers) {
        $scores = [];
        foreach ($this->dimensions as $key => $dim) {
            $dimScore = 0;
            foreach ($dim['questions'] as $q) {
                if (!isset($answers[$q['id']])) continue;
                
                $answer = $answers[$q['id']];
                // 处理反向计分
                $effectiveScore = ($q['type'] == 'negative') ? 6 - $answer : $answer;
                $dimScore += $effectiveScore * $dim['weight'];
            }
            // 转换为百分制
            $maxScore = 5 * count($dim['questions']) * $dim['weight'];
            $scores[$key] = round(($dimScore / $maxScore) * 100, 1);
        }
       
		
		
		
		$dominantStyle = $this->determineDominantStyle($scores);
		$total_index = $dominantStyle['score'];
		$level = $dominantStyle['type'];
		//建议
		$suggestion = [];
		$adviceList = $this->generateAdvice($scores);
		foreach ($adviceList as $advice) {
			$suggestion[] = "{$advice['style']} 优势：{$advice['strength']}。建议：{$advice['suggestion']}";
		}
		//分项评估
		$dimensionScores = [];
		foreach ($scores as $dim => $score) {
			$dimName = $this->dimensions[$dim]['name'];
			$dimensionScores[$dimName] = "{$score}分";
		}
		return [
            'score' => $total_index,
            'level' => $level,
            'suggestion' => $suggestion,
            'dimensions' => $dimensionScores
        ];
    }

    // 判断主导教育方式
    public function determineDominantStyle($scores) {
        arsort($scores);
        $topStyle = key($scores);
        return [
            'type' => $this->dimensions[$topStyle]['name'],
            'score' => $scores[$topStyle]
        ];
    }

    // 生成教育建议
    public function generateAdvice($scores) {
        $advice = [];
        foreach ($scores as $key => $score) {
            if ($score > 60) {
                $advice[] = $this->getDimensionAdvice($key, $score);
            }
        }
        return $advice;
    }

    private function getDimensionAdvice($dimKey, $score) {
        $adviceMap = [
            'authoritative' => [
                '优势' => '良好的规则与沟通平衡',
                '建议' => '继续保持情感支持，注意避免过度民主'
            ],
            'authoritarian' => [
                '优势' => '明确的纪律要求', 
                '建议' => '增加孩子参与决策的机会，多倾听孩子想法'
            ],
            'permissive' => [
                '优势' => '给予孩子较大自主空间',
                '建议' => '建立基本规则框架，加强责任意识培养'
            ],
            'neglectful' => [
                '优势' => '孩子独立性较强',
                '建议' => '增加日常互动时间，定期安排家庭活动'
            ]
        ];
        return [
            'style' => $this->dimensions[$dimKey]['name'],
            'strength' => $adviceMap[$dimKey]['优势'],
            'suggestion' => $adviceMap[$dimKey]['建议']
        ];
    }
}

?>